#include <iostream>
#include <string>

using std::string;

// 抽象原型类
class Object {
public:
    virtual Object* clone() = 0;
};

// 邮件的附件
class Attachment {
public:
    void setContent(string content) { content_ = content; }
    string getContent() { return content_; }

private:
    string content_;
};

// 具体原型：邮件类
class Email : public Object {
public:
    Email() {};
    Email(string text, string attachmentContent)
        : text_(text), attachment_(new Attachment()) {
        attachment_->setContent(attachmentContent);
    }
    ~Email() {
        if (attachment_ != nullptr) {
            delete attachment_;
            attachment_ = nullptr;
        }
    }

    void display() {
        std::cout << "text: " << text_ << std::endl;
        std::cout << "attachment: " << attachment_->getContent() << std::endl;
    }

    Email* clone() override {
        return new Email(this->text_, this->attachment_->getContent());
    }

    void changeText(string newText) { text_ = newText; }

    void changeAttachment(string content) { attachment_->setContent(content); }

private:
    string text_ = "";
    Attachment* attachment_ = nullptr;
};

int main() {
    Email* email = new Email("init text", "init attachment");
    std::cout << "===== New =====" << std::endl;
    for (int i = 0; i < 10; ++i) {
        Email* copyEmail = email->clone();
        copyEmail->changeText("new text" + std::to_string(i));
        copyEmail->changeAttachment("new attachment" + std::to_string(i));
        copyEmail->display();
        delete copyEmail;
    }
    std::cout << "===== Prototype =====" << std::endl;
    email->display();
    delete email;
}